package com.zyf.chapter01.principle.SOLID.SRP;

import com.zyf.chapter01.principle.SOLID.SRP.utils.Gson;

import java.util.Collections;
import java.util.Map;

/**
 * 序列化类，包含序列化和反序列化
 * @author yifeng
 */
public class Serialization {
    private static final String IDENTIFIER_STRING = "UEUEUE;";
    private Gson gson;
    
    public Serialization() {
        this.gson = new Gson();
    }
    
    public String Serialize(Map<String, String> object) {
        StringBuilder textBuilder = new StringBuilder();
        textBuilder.append(IDENTIFIER_STRING);
        textBuilder.append(gson.toJson(object));
        return textBuilder.toString();
    }
    
    public Map<String, String> serialize(String text) {
        if(!text.startsWith(IDENTIFIER_STRING)) {
            return Collections.emptyMap();
        }
        String gsonStr = text.substring(IDENTIFIER_STRING.length());
        return gson.fromJson(gsonStr, Map.class);
    }
}